package com.miao.controller;

import com.alibaba.fastjson.JSON;
import com.miao.dto.CourseBaseInfoDto;
import com.miao.dto.CoursePreviewDto;
import com.miao.dto.TeachPlanDto;
import com.miao.pojo.CoursePublish;
import com.miao.service.ICoursePublishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Slf4j
@Controller
public class CoursePublishController {

    @Autowired
    private ICoursePublishService coursePublishService;

    //课程预览接口
    @GetMapping("/content/coursepreview/{courseId}")
    public ModelAndView preview(@PathVariable Long courseId){
        log.info("有人访问了！！！！fuck");
        //获取模板文件
        ModelAndView modelAndView = new ModelAndView();
        //获取课程信息
        CoursePreviewDto coursePreview = coursePublishService.getCoursePreview(courseId);
        modelAndView.addObject("model", coursePreview);
        modelAndView.setViewName("course_template");
        return modelAndView;
    }

    //提交课程(审核)的接口
    @ResponseBody
    @PostMapping("/content/courseaudit/commit/{courseId}")
    public void commitAudit(@PathVariable Long courseId){
        Long comId = 1232141425L;
        coursePublishService.commitAuditCourse(courseId, comId);
    }

    //发布课程的接口
    @ResponseBody
    @PostMapping("/content/coursepublish/{courseId}")
    public void coursePublish(@PathVariable Long courseId){
        Long comId = 1232141425L;
        coursePublishService.publishCourse(courseId, comId);
    }

    //查看发布课程
    @ResponseBody
    @GetMapping("/content/r/coursepublish/{courseId}")
    public CoursePublish getCoursePublish(@PathVariable("courseId") Long courseId){
        return coursePublishService.getById(courseId);
    }

    //获取课程发布信息
    @ResponseBody
    @GetMapping("/content/course/whole/{courseId}")
    public CoursePreviewDto getCoursePublishDto(@PathVariable("courseId") Long courseId) {
        //查询课程发布信息
//        CoursePublish coursePublish = coursePublishService.getById(courseId);
        CoursePublish coursePublish = coursePublishService.getCoursePublishCache(courseId);//reids
        if (coursePublish == null) {
            return new CoursePreviewDto();
        }

        //课程基本信息
        CourseBaseInfoDto courseBase = new CourseBaseInfoDto();
        BeanUtils.copyProperties(coursePublish, courseBase);
        //课程计划
        List<TeachPlanDto> teachplans = JSON.parseArray(coursePublish.getTeachplan(), TeachPlanDto.class);
        CoursePreviewDto coursePreviewInfo = new CoursePreviewDto();
        coursePreviewInfo.setCourseBase(courseBase);
        coursePreviewInfo.setTeachplans(teachplans);
        return coursePreviewInfo;
    }


}
